//
//  UIView+GEE_fastInit.m
//  GeelyApp
//
//  Created by LJQ on 2021/4/23.
//  Copyright © 2021 Geely. All rights reserved.
//

#import "UIView+HYC_Tools.h"
#import <objc/runtime.h>

@interface UIView (HYC_Tools) <UIGestureRecognizerDelegate>

@end


static NSString *tapKey = @"tapKey";



@implementation UIView (Tools_HYC_Neusoft)
- (UIView *(^)(CGRect T)) H_frame {
    return ^id(CGRect T){
        self.frame = T;
        return self;
        };
}
- (UIView *(^)(UIView * T)) H_addSubViewFoSuper {
    return ^id(UIView * T){
        [T addSubview:self];
        return self;
        };
}
- (UIImage *)H_image {
    [self layoutIfNeeded];
    return [self convertViewToImage:self];
}
//使用该方法不会模糊，根据屏幕密度计算
- (UIImage *)convertViewToImage:(UIView *)view {
    
    UIImage *imageRet = [[UIImage alloc]init];
    //UIGraphicsBeginImageContextWithOptions(区域大小, 是否是非透明的, 屏幕密度);
    UIGraphicsBeginImageContextWithOptions(view.frame.size, YES, [UIScreen mainScreen].scale);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    imageRet = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return imageRet;
    
}

/// 修改部分圆角
/// @param radius 圆角值
/// @param corner 圆角位置
-(void)setLayers:(CGFloat)radius type:(UIRectCorner)corner {
    if (@available(iOS 11.0, *)) {
        self.layer.cornerRadius = radius;
        self.layer.maskedCorners = (CACornerMask)corner;
    } else {
        UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corner cornerRadii:CGSizeMake(radius, radius)];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = self.bounds;
        maskLayer.path = path.CGPath;
        self.layer.mask = maskLayer;
    }
    self.layer.masksToBounds = true;
}

-(void)setLayers:(CGFloat)radius {
    self.layer.cornerRadius = radius;
    self.layer.masksToBounds = true;
}

- colorLayer_GradientWith:(UIColor *)leftColor With:(UIColor *)rightColor{
    
    [self layoutIfNeeded];
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
        gradientLayer.colors = @[ (__bridge id)leftColor.CGColor, (__bridge id)rightColor.CGColor];
        gradientLayer.locations = @[@0.0, @1.0];
        gradientLayer.startPoint = CGPointMake(0, 0);
        gradientLayer.endPoint = CGPointMake(1.0, 0);
        gradientLayer.frame = self.frame;
        [self.layer addSublayer:gradientLayer];
    
    return self;
}
- (void)callBlock:(UIGestureRecognizer *)gestureType {
    Block guestBlock = objc_getAssociatedObject(self, &tapKey);
    if (guestBlock) {
        guestBlock(gestureType);
    }
    //    return objc_getAssociatedObject(self, @selector(callBlock));
}





/// View添加点击事件
/// @param enumType 点击类型
/// @param block 回调
-(instancetype)addGuest:(GestureENUM)enumType resutlBlock:(Block)block {
    [self setUserInteractionEnabled:YES];
    objc_setAssociatedObject(self, &tapKey, block, OBJC_ASSOCIATION_COPY);
    [[self class] cancelPreviousPerformRequestsWithTarget:self selector:@selector(callBlock:) object:nil];
    if (enumType == tapAction) {
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(callBlock:)];
        tap.numberOfTapsRequired = 1;
        [self addGestureRecognizer:tap];
    }else if (enumType == longAction) {
        UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(callBlock:)];
        [self addGestureRecognizer:longGesture];
    }else if (enumType == panAction) {
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(callBlock:)];
        [self addGestureRecognizer:pan];
    }else if (enumType == roationAction) {
        UIRotationGestureRecognizer *roation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(callBlock:)];
        [self addGestureRecognizer:roation];
    }else if (enumType == swipeAction) {
        UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(callBlock:)];
        [self addGestureRecognizer:swipe];
    }else if (enumType == pinchAction) {
        UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(callBlock:)];
        [self addGestureRecognizer:pinch];
    }
    
    return self;
}



- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return YES;
}

@end
